{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 跳空接口：\n",
    "#  普通缺口：特点是很快会被回补，价格在几天内就会回补；\n",
    "#  突破缺口：当价格和成交量伴随跳空 跳出震荡区，则预示着新趋势的形成\n",
    "# 衰竭缺口：缺口没有很快回补，走势也反复无常，最终会慢慢回补缺口，预示着市场走势将可能会有剧烈的反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取缺口的方式：\n",
    "# 1，如果向上，则今天的最低价要高于昨天收盘价格一个阀值上，确定向上跳空；\n",
    "# 2, 今天如果下跌，那么跳空的确定需要昨天收盘价格大于今天最高价格一个阀值以上，确定向下跳空。\n",
    "#  这种方式确定的跳空缺口纯在很强的支撑或者阻力，首先确定阀值，这里计算方式是使用统计周期内收盘价格的中位数乘以3%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "tsla_dt = pd.read_csv('./tsla_2.csv', parse_dates=True, index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "jump_threshold = tsla_dt.close.median()*0.03\n",
    "\n",
    "# 新建一个表格来存储跳空的数据 jump_pd \n",
    "# 条件1， jump 代表跳空的方向， \n",
    "# 条件2， jump_power 代表跳空的能量，这里能量的计算是由缺口的高度除以阀值获得，能量再次量化了支撑和阻力的大小；\n",
    "\n",
    "jump_pd = pd.DataFrame()\n",
    "def judge_jump(today):\n",
    "    global jump_pd\n",
    "    if today.p_change > 0 and (today.low - today.close) > jump_threshold:\n",
    "            '''符合向上跳空'''\n",
    "            today['jump'] = 1 \n",
    "            '''跳空能量 = （今日最低-昨收）/跳空阀值'''\n",
    "            today['jump_power'] = (today.low - today.close)/jump_threshold\n",
    "            jump_pd = jump_pd.append(today)\n",
    "    elif today.p_change < 0 and (today.close - today.high)> jump_threshold:\n",
    "        '''符合向下跳空'''\n",
    "        today['jump'] = -1\n",
    "        '''向下跳空能量'''\n",
    "        today['jump_power'] = (today.close - today.high)/ jump_threshold\n",
    "        jump_pd.append(today)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for kl_index in np.arange(0, tsla_dt.shape[0]):\n",
    "    today=tsla_dt.iloc[kl_index]\n",
    "    judge_jump(today)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# filter  按照顺序只展示这些列\n",
    "jump_pd.filter(['jump', 'jump_power','close','date','p_change'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
